<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 会员管理
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-02-25 13:43:37 +0800 (二, 2014-02-25) $
 * @version : $Id: member.php 31 2014-02-25 05:43:37Z spike $
 * @rev     : $Revision: 31 $
 */
class Member extends CI_Controller 
{  
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * [浏览]
     */
    public function browse()
    {
        $params = $this->input->get();
        if($params)
        {
            $this->load->model('Member_model');
            $list = $this->Member_model->browse_member($params, $params['page'], $params['size']);

            $this->load->library('pagination');
            $config['total_rows'] = $list['total'];
            $config['per_page']   = $list['size'];
            $config['suffix']     = '#data-table';

            $this->pagination->initialize($config); 
            $pagination = $this->pagination->create_links();
        }

        // 统计
        $member_status = $this->config->item('member_status');
        $member_gender = $this->config->item('member_gender');

        $this->load->dao('Member_dao');
        $total = $this->Member_dao->count(array(
            'is_delete' => UNDELETED
        ));

        // 筛选
        $pagesize     = $this->config->item('pagesize');

        $data = array(
            'query'         => $params ? http_build_query($params) : '',
            'default'       => $params ? $params : array(),
            'total'         => number_format($total),
            'list'          => $list,
            'pagination'    => $pagination,
            'pagesize'      => $pagesize,
            'member_status' => $member_status,
            'member_gender' => $member_gender,
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('member/browse.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [新增会员]
     */
    public function add()
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            temp('add_member', $params);

            $this->load->model('Member_model');

            if( FALSE === $member_id = $this->Member_model->add_member($params) )
            {
                show_error($this->Member_model->get_error(), 200, '错误', 'error_general_lite');
            }
            temp('add_member', NULL);

            show_message(
                "新增会员成功！", 
                array('查看新建的会员' => DOMAIN."member/view/{$member_id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $member_status = $this->config->item('member_status');
            $member_gender = $this->config->item('member_gender');
            $page_style    = $this->config->item('page_style');
            $data = array(
                'temp'          => temp('add_member'),
                'page_style'    => $page_style['LITE'],
                'member_gender' => $member_gender, 
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('member/add.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [改变会员状态]
     */
    public function change_status($id = NULL)
    {
        $act     = $this->input->get('act');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Member_model');
        if( FALSE === $this->Member_model->change_member_status($id, $act))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Member_model->get_error());
            }
            else
            {
                show_error($this->Member_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
            else
            {
                show_message("改变会员状态成功！", array(
                    '返回会员管理' => DOMAIN."member/browse",
                ));
            }
        }
    }

    /**
     * [查看单个会员信息]
     */
    public function view($id = NULL)
    {
        $this->load->model('Member_model');
        if( ! $member = $this->Member_model->get_member($id, FALSE))
        {
            show_error($this->Member_model->get_error(), 200, '错误', 'error_general_lite');
        }

        // 绑定操作人数据
        $this->load->model('Rbac_model');
        $this->Rbac_model->wrap_admin_info($member, TRUE, 'create_admin');
        $this->Rbac_model->wrap_admin_info($member, TRUE, 'update_admin');

        $page_style = $this->config->item('page_style');
        $data = array(
            'member'        => $member,
            'member_gender' => $this->config->item('admin_gender'),
            'member_status' => $this->config->item('admin_status'),
            'admin_status'  => $admin_status,
            'page_style'    => $page_style['LITE_GREY'],
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('member/view.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [编辑会员]
     */
    public function edit($id = NULL)
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            $this->load->model('Member_model');

            if( FALSE === $this->Member_model->edit_member($id, $params) )
            {
                show_error($this->Member_model->get_error(), 200, '错误', 'error_general_lite');
            }

            show_message(
                "编辑会员成功！", 
                array('查看编辑的会员' => DOMAIN."member/view/{$id}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Member_model');
            if( ! $member = $this->Member_model->get_member($id, FALSE))
            {
                show_error($this->Member_model->get_error(), 200, '错误', 'error_general_lite');
            }

            $page_style    = $this->config->item('page_style');
            $data = array(
                'member'        => $member,
                'page_style'    => $page_style['LITE'],
                'member_gender' => $this->config->item('member_gender'), 
                'member_status' => $this->config->item('member_status'),
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('member/edit.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [浏览]
     */
    public function export()
    {
        $params = $this->input->get();

        $export_page = (int)$params['export_page']; 
        $export_size = 5000;

        // 如果没有分页参数
        if( $export_page <= 0 )
        {
            $this->load->model('Member_model');
            $count = $this->Member_model->count_member($params);
            if( ! ($count>0) )
            {
                show_error("导出会员失败：无法找到符合条件的数据。", 200, '错误', 'error_general_lite');
            }
            $split = array();
            $times = (int)($count/$export_size);

            $page_style    = $this->config->item('page_style');
            $data = array(
                'title'       => "会员数据",
                'times'       => $times,
                'count'       => $count,
                'export_size' => $export_size,
                'page_style'  => $page_style['LITE'],
                'query'       => http_build_query($params),
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('excel/export.tpl');
            $this->load->view('home/footer.tpl');
        }
        else
        {
            $this->load->model('Member_model');
            $list = $this->Member_model->browse_member($params,$export_page, $export_size, NULL, TRUE);

            $title = array(
                '会员ID',
                '状态',
                '账户名',
                '昵称',
                '手机',
                '性别',
                '备注',
                '上次登录',
                '创建时间',
                '创建人',
                '更新时间',
                '更新人',
                '是否删除',
            );
            $filename = $list['total'] > $export_size ? "Member.".date('Ymd').".Part{$export_page}" :  "Member.".date('Ymd');

            array_unshift($list['data'], $title);
            $this->load->model('Excel_model');
            $this->Excel_model->export($list['data'], '会员数据', $filename);
        }
    }

}
/* End of file */